<?php

namespace app\api\ctrl;

class LotteryCtrl
{
    /**
     * http://127.0.0.1:82/api/lottery/testGetRand
     * @return void
     */
    public function testGetRand() {
        //本次抽奖的奖项信息，必须按照从大到小的顺序进行填写，id为奖次，prize为中奖信息,v为中奖概率,num为奖品数量
        //需要注意的是，该处也必须包含不中奖的信息，概率从小到大进行排序
        $prize_arr = [
            ['id' => 2, 'prize' => '55元购买1G/年空间', 'v' => 3, 'num' => 3],
            ['id' => 4, 'prize' => '77元购买1G/年空间', 'v' => 15, 'num' => 5],
            ['id' => 6, 'prize' => '99元购买1G/年空间', 'v' => 82, 'num' => 14],
        ];

        $total = 50;
        while($total > 0) {
            $proArr = array_column($prize_arr, 'v', 'id');
            // 根据概率获取奖项id
            $rid = \app\service\LotteryCollect::get_rand($proArr);
            $str = $prize_arr[$rid - 2]['prize'];
            echo '中奖：' . $str . "\r\n";
            $prize_arr[$rid - 2]['num']--;
            $total--;
        }
    }

}